// SmarTest Studio - ReportTab类

import { showNotification } from './utils.js';
import http from './http-client.js';

/**
 * ReportTab类 - 负责管理报告标签页的功能
 */
export class ReportTab {
    constructor() {
        // 绑定方法到实例
        this.loadReports = this.loadReports.bind(this);
        this.getReportStatusClass = this.getReportStatusClass.bind(this);
    }
    
    /**
     * 加载报告列表
     */
    loadReports() {
        http.get('/api/reports?api=get_reports')
            .then((response) => {
                const reports = response.data.reports;
                const tableBody = document.getElementById('reportsTable');
                tableBody.innerHTML = '';

                reports.forEach((report) => {
                    const row = document.createElement('tr');
                    row.innerHTML = `
                        <td>
                            <a href="/report/${report.id}" class="report-link">${report.name}</a>
                        </td>
                        <td>${report.owner}</td>
                        <td>${report.testcase}</td>
                        <td>${report.device}</td>
                        <td>${report.created}</td>
                        <td>
                            <span class="status-badge ${this.getReportStatusClass(report.status)}">${report.status}</span>
                        </td>
                        <td>
                            <div class="btn-group">
                                <button class="btn btn-sm btn-outline-primary" title="查看">
                                    <i class="bi bi-eye"></i>
                                </button>
                                <button class="btn btn-sm btn-outline-secondary" title="导出">
                                    <i class="bi bi-download"></i>
                                </button>
                                <button class="btn btn-sm btn-outline-danger" title="删除">
                                    <i class="bi bi-trash"></i>
                                </button>
                            </div>
                        </td>
                    `;
                    tableBody.appendChild(row);
                });
            })
            .catch((error) => {
                console.error('加载报告失败:', error);
                showNotification('加载报告失败', 'danger');
            });
    }
    
    /**
     * 获取报告状态样式类
     * @param {string} status - 状态值
     * @returns {string} - 对应的CSS类名
     */
    getReportStatusClass(status) {
        switch (status) {
            case '通过':
                return 'status-passed';
            case '失败':
                return 'status-failed';
            case '部分通过':
                return 'status-partial';
            default:
                return '';
        }
    }
}